package br.com.mural.android.adaptadores;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import br.com.mural.android.R;
import br.com.mural.android.entidades.AlertaAusencia;
import br.com.mural.android.entidades.AlertaEmprego;
import br.com.mural.android.entidades.AlertaProva;
import br.com.mural.android.entidades.AlertaTroca;
import br.com.mural.android.entidades.Aviso;

public class AvisoAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	ItemSuporte itemHolder;
	private List<Aviso> avisos;

	public AvisoAdapter(Context context, List<Aviso> avisos) {
		this.avisos = avisos;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return avisos.size();
	}

	@Override
	public Object getItem(int position) {
		return avisos.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View view, ViewGroup parent) {
		

		if (view == null) {
			view = inflater.inflate(R.layout.item_aviso, null);

			itemHolder = new ItemSuporte();
			itemHolder.txtDescricao = ((TextView) view.findViewById(R.id.aviso));
			itemHolder.txtUsuario = ((TextView) view.findViewById(R.id.usuario));
			itemHolder.imgTipo = ((ImageView) view.findViewById(R.id.imgTipoAviso));

			
			
			
			view.setTag(itemHolder);
		} else {

			itemHolder = (ItemSuporte) view.getTag();
		}

		Aviso item = avisos.get(position);
		
		itemHolder.txtDescricao.setText(item.getDescricao());
		itemHolder.txtUsuario.setText(item.getUsuario().getNome());
		if (item instanceof AlertaAusencia) {
			itemHolder.imgTipo.setImageResource(R.drawable.ic_alerta_ausencia);
		} else {
			if (item instanceof AlertaEmprego) {
				itemHolder.imgTipo.setImageResource(R.drawable.ic_alerta_emprego);
			} else {
				if (item instanceof AlertaProva) {
					itemHolder.imgTipo.setImageResource(R.drawable.ic_alerta_prova);
				} else {
					if (item instanceof AlertaTroca) {
						itemHolder.imgTipo.setImageResource(R.drawable.ic_alerta_troca);
					} 
				}
			}
		}
		
		if(item.getDescricao().length() > 50){
			ImageView imgMaisInformacoes = ((ImageView) view.findViewById(R.id.imgMaisInformacoes));
			imgMaisInformacoes.setVisibility(View.VISIBLE);
		}
		

		if (position % 2 == 0) {
			view.setBackgroundColor(Color.GRAY);
		} else {
			view.setBackgroundColor(Color.LTGRAY);
		}
		return view;
	}

	public class ItemSuporte {
		TextView txtDescricao;
		ImageView imgTipo;
		TextView txtUsuario;
	}

}
